<template>
  <el-drawer :title="dialogTitle" v-model="dialogVisible" size="70%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="机构ID" prop="orgId">
        <el-input v-model="formData.orgId" placeholder="请输入机构ID" />
      </el-form-item>
      <el-form-item label="机构名称" prop="orgName">
        <el-input v-model="formData.orgName" placeholder="请输入机构名称" />
      </el-form-item>
      <el-form-item label="机构编码" prop="orgCode">
        <el-input v-model="formData.orgCode" placeholder="请输入机构编码" />
      </el-form-item>
      <el-form-item label="申请单号" prop="applyNo">
        <el-input v-model="formData.applyNo" placeholder="请输入申请单号" />
      </el-form-item>
      <el-form-item label="主页ID" prop="homepageId">
        <el-input v-model="formData.homepageId" placeholder="请输入主页ID" />
      </el-form-item>
      <el-form-item label="出院日期" prop="leaveHospitalDate">
        <el-date-picker
          v-model="formData.leaveHospitalDate"
          type="date"
          value-format="x"
          placeholder="选择出院日期"
        />
      </el-form-item>
      <el-form-item label="出院科室" prop="leaveHospitalDept">
        <el-input v-model="formData.leaveHospitalDept" placeholder="请输入出院科室" />
      </el-form-item>
      <el-form-item label="出院证明" prop="leaveHospitalCertificate">
        <el-input v-model="formData.leaveHospitalCertificate" placeholder="请输入出院证明" />
      </el-form-item>
      <el-form-item label="住院号" prop="hospitalNo">
        <el-input v-model="formData.hospitalNo" placeholder="请输入住院号" />
      </el-form-item>
      <el-form-item label="病案类型" prop="medicalType">
        <el-select v-model="formData.medicalType" placeholder="请选择病案类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="用途" prop="purpose">
        <el-input v-model="formData.purpose" placeholder="请输入用途" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <Editor v-model="formData.content" height="150px" />
      </el-form-item>
      <el-form-item label="申请人与病人关系" prop="relation">
        <el-input v-model="formData.relation" placeholder="请输入申请人与病人关系" />
      </el-form-item>
      <el-form-item label="病人ID" prop="patientId">
        <el-input v-model="formData.patientId" placeholder="请输入病人ID" />
      </el-form-item>
      <el-form-item label="病人类型" prop="patientType">
        <el-select v-model="formData.patientType" placeholder="请选择病人类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="病人姓名" prop="patientName">
        <el-input v-model="formData.patientName" placeholder="请输入病人姓名" />
      </el-form-item>
      <el-form-item label="病人证件号" prop="patientCardNo">
        <el-input v-model="formData.patientCardNo" placeholder="请输入病人证件号" />
      </el-form-item>
      <el-form-item label="病人身份证人像面" prop="patientCardImgFront">
        <el-input v-model="formData.patientCardImgFront" placeholder="请输入病人身份证人像面" />
      </el-form-item>
      <el-form-item label="病人身份证国徽面" prop="patientCardImgReverse">
        <el-input v-model="formData.patientCardImgReverse" placeholder="请输入病人身份证国徽面" />
      </el-form-item>
      <el-form-item label="申请人姓名" prop="applicantName">
        <el-input v-model="formData.applicantName" placeholder="请输入申请人姓名" />
      </el-form-item>
      <el-form-item label="申请人证件号" prop="applicantCardNo">
        <el-input v-model="formData.applicantCardNo" placeholder="请输入申请人证件号" />
      </el-form-item>
      <el-form-item label="申请人身份证人像面" prop="applicantCardImgFront">
        <el-input v-model="formData.applicantCardImgFront" placeholder="请输入申请人身份证人像面" />
      </el-form-item>
      <el-form-item label="申请人身份证国徽面" prop="applicantCardImgReverse">
        <el-input v-model="formData.applicantCardImgReverse" placeholder="请输入申请人身份证国徽面" />
      </el-form-item>
      <el-form-item label="其他文件" prop="otherFile">
        <UploadFile v-model="formData.otherFile" />
      </el-form-item>
      <el-form-item label="收取方式" prop="collectType">
        <el-select v-model="formData.collectType" placeholder="请选择收取方式">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="收件人姓名" prop="recipientName">
        <el-input v-model="formData.recipientName" placeholder="请输入收件人姓名" />
      </el-form-item>
      <el-form-item label="收件人电话" prop="recipientPhone">
        <el-input v-model="formData.recipientPhone" placeholder="请输入收件人电话" />
      </el-form-item>
      <el-form-item label="收件人地址" prop="recipientAddress">
        <el-input v-model="formData.recipientAddress" placeholder="请输入收件人地址" />
      </el-form-item>
      <el-form-item label="快递公司ID" prop="deliveryId">
        <el-input v-model="formData.deliveryId" placeholder="请输入快递公司ID" />
      </el-form-item>
      <el-form-item label="快递公司" prop="deliveryName">
        <el-input v-model="formData.deliveryName" placeholder="请输入快递公司" />
      </el-form-item>
      <el-form-item label="快递单号" prop="deliveryNo">
        <el-input v-model="formData.deliveryNo" placeholder="请输入快递单号" />
      </el-form-item>
      <el-form-item label="支付订单号" prop="orderNumber">
        <el-input v-model="formData.orderNumber" placeholder="请输入支付订单号" />
      </el-form-item>
      <el-form-item label="渠道订单号" prop="thirdOrderNo">
        <el-input v-model="formData.thirdOrderNo" placeholder="请输入渠道订单号" />
      </el-form-item>
      <el-form-item label="订单金额" prop="totalPrice">
        <el-input v-model="formData.totalPrice" placeholder="请输入订单金额" />
      </el-form-item>
      <el-form-item label="病案费用" prop="medicalPrice">
        <el-input v-model="formData.medicalPrice" placeholder="请输入病案费用" />
      </el-form-item>
      <el-form-item label="打印费用" prop="printPrice">
        <el-input v-model="formData.printPrice" placeholder="请输入打印费用" />
      </el-form-item>
      <el-form-item label="邮寄费用" prop="postagePrice">
        <el-input v-model="formData.postagePrice" placeholder="请输入邮寄费用" />
      </el-form-item>
      <el-form-item label="服务费用" prop="servicePrice">
        <el-input v-model="formData.servicePrice" placeholder="请输入服务费用" />
      </el-form-item>
      <el-form-item label="其他费用" prop="otherPrice">
        <el-input v-model="formData.otherPrice" placeholder="请输入其他费用" />
      </el-form-item>
      <el-form-item label="0 未支付 1 支付成功 2 已退款 3 申请退款" prop="payStatus">
        <el-radio-group v-model="formData.payStatus">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          v-model="formData.payTime"
          type="date"
          value-format="x"
          placeholder="选择支付时间"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="formData.payType" placeholder="请选择支付方式">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="退款订单号" prop="refundNumber">
        <el-input v-model="formData.refundNumber" placeholder="请输入退款订单号" />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundPrice">
        <el-input v-model="formData.refundPrice" placeholder="请输入退款金额" />
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker
          v-model="formData.refundTime"
          type="date"
          value-format="x"
          placeholder="选择退款时间"
        />
      </el-form-item>
      <el-form-item label="退款原因" prop="refundReason">
        <el-input v-model="formData.refundReason" placeholder="请输入退款原因" />
      </el-form-item>
      <el-form-item label="审核意见" prop="examine">
        <el-input v-model="formData.examine" placeholder="请输入审核意见" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="病人身份证手持照" prop="patientCardImgHandheld">
        <el-input v-model="formData.patientCardImgHandheld" placeholder="请输入病人身份证手持照" />
      </el-form-item>
      <el-form-item label="申请人身份证手持照" prop="applicantCardImgHandheld">
        <el-input v-model="formData.applicantCardImgHandheld" placeholder="请输入申请人身份证手持照" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { MedicalCopyApi, MedicalCopyVO } from '@/api/hospital/medicalcopy'

/** 病案复印 表单 */
defineOptions({ name: 'MedicalCopyForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  orgId: undefined,
  orgName: undefined,
  orgCode: undefined,
  applyNo: undefined,
  homepageId: undefined,
  leaveHospitalDate: undefined,
  leaveHospitalDept: undefined,
  leaveHospitalCertificate: undefined,
  hospitalNo: undefined,
  medicalType: undefined,
  purpose: undefined,
  content: undefined,
  relation: undefined,
  patientId: undefined,
  patientType: undefined,
  patientName: undefined,
  patientCardNo: undefined,
  patientCardImgFront: undefined,
  patientCardImgReverse: undefined,
  applicantName: undefined,
  applicantCardNo: undefined,
  applicantCardImgFront: undefined,
  applicantCardImgReverse: undefined,
  otherFile: undefined,
  collectType: undefined,
  recipientName: undefined,
  recipientPhone: undefined,
  recipientAddress: undefined,
  deliveryId: undefined,
  deliveryName: undefined,
  deliveryNo: undefined,
  orderNumber: undefined,
  thirdOrderNo: undefined,
  totalPrice: undefined,
  medicalPrice: undefined,
  printPrice: undefined,
  postagePrice: undefined,
  servicePrice: undefined,
  otherPrice: undefined,
  payStatus: undefined,
  payTime: undefined,
  payType: undefined,
  refundNumber: undefined,
  refundPrice: undefined,
  refundTime: undefined,
  refundReason: undefined,
  examine: undefined,
  remark: undefined,
  status: undefined,
  patientCardImgHandheld: undefined,
  applicantCardImgHandheld: undefined
})
const formRules = reactive({
  orgName: [{ required: true, message: '机构名称不能为空', trigger: 'blur' }],
  applyNo: [{ required: true, message: '申请单号不能为空', trigger: 'blur' }],
  medicalType: [{ required: true, message: '病案类型不能为空', trigger: 'change' }],
  purpose: [{ required: true, message: '用途不能为空', trigger: 'blur' }],
  content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
  relation: [{ required: true, message: '申请人与病人关系不能为空', trigger: 'blur' }],
  patientId: [{ required: true, message: '病人ID不能为空', trigger: 'blur' }],
  patientName: [{ required: true, message: '病人姓名不能为空', trigger: 'blur' }],
  patientCardNo: [{ required: true, message: '病人证件号不能为空', trigger: 'blur' }],
  collectType: [{ required: true, message: '收取方式不能为空', trigger: 'change' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MedicalCopyApi.getMedicalCopy(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MedicalCopyVO
    if (formType.value === 'create') {
      await MedicalCopyApi.createMedicalCopy(data)
      message.success(t('common.createSuccess'))
    } else {
      await MedicalCopyApi.updateMedicalCopy(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    orgId: undefined,
    orgName: undefined,
    orgCode: undefined,
    applyNo: undefined,
    homepageId: undefined,
    leaveHospitalDate: undefined,
    leaveHospitalDept: undefined,
    leaveHospitalCertificate: undefined,
    hospitalNo: undefined,
    medicalType: undefined,
    purpose: undefined,
    content: undefined,
    relation: undefined,
    patientId: undefined,
    patientType: undefined,
    patientName: undefined,
    patientCardNo: undefined,
    patientCardImgFront: undefined,
    patientCardImgReverse: undefined,
    applicantName: undefined,
    applicantCardNo: undefined,
    applicantCardImgFront: undefined,
    applicantCardImgReverse: undefined,
    otherFile: undefined,
    collectType: undefined,
    recipientName: undefined,
    recipientPhone: undefined,
    recipientAddress: undefined,
    deliveryId: undefined,
    deliveryName: undefined,
    deliveryNo: undefined,
    orderNumber: undefined,
    thirdOrderNo: undefined,
    totalPrice: undefined,
    medicalPrice: undefined,
    printPrice: undefined,
    postagePrice: undefined,
    servicePrice: undefined,
    otherPrice: undefined,
    payStatus: undefined,
    payTime: undefined,
    payType: undefined,
    refundNumber: undefined,
    refundPrice: undefined,
    refundTime: undefined,
    refundReason: undefined,
    examine: undefined,
    remark: undefined,
    status: undefined,
    patientCardImgHandheld: undefined,
    applicantCardImgHandheld: undefined
  }
  formRef.value?.resetFields()
}
</script>
